//
//  webTool.m
//  SHZDemo
//
//  Created by dyj on 16/5/20.
//  Copyright © 2016年 Tangshan Jun-ho Technology Co.Ltd. All rights reserved.
//

#import "webTool.h"
#import <UIKit/UIKit.h>
#import "JFTConfigureHeader.h"
static int a;
@class webTool;
@interface webTool ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *myWebView;
@end
@implementation webTool
webTool *tool=nil;

+(instancetype)shareInstsnype{
    if (!tool) {
        tool=[[webTool alloc]init];
    }
    a=0;
    return tool;
    
}
+ (UIWebView *)WithUrlString:(NSString *)string{
    UIWebView *web=[[UIWebView alloc]init];
    web.delegate=tool;
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
    return web;
}
-(void)gety:(NSString *)mystr
{
   self.myWebView=[webTool WithUrlString:mystr];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    if (a==4) {
        self.block(@"1");
    }
    if (a==0) {
        self.block(@"0");
    }
    a++;
 
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSURL *myUrl = error.userInfo[@"NSErrorFailingURLKey"];
    if ([[myUrl.absoluteString substringToIndex:7] isEqualToString:@"alipays"]||[[myUrl.absoluteString substringToIndex:6] isEqualToString:@"weixin"]) {
        [self openAppWithUrlString:myUrl];
    }
}
- (void)openAppWithUrlString:(NSURL *)string{
    if (JFTIOS_VERSION_10) {
        [[UIApplication sharedApplication]openURL:string options:@{} completionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"打开成功");
            }else
                NSLog(@"打开失败");
            
        }];
    }else
        
        [[UIApplication sharedApplication] openURL:string];

  
}
@end
